package fr.afcepf.ai85.gepeto.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import fr.afcepf.ai85.ent.MessageEnvoye;
import fr.afcepf.ai85.ent.MessageRecu;
import fr.afcepf.ai85.hibernate.util.HibernateUtil;

public class ServiceMessageRecu {
	
	public static List<MessageRecu> listeMessageRecu(Integer IdEnfant){	
		List<MessageRecu> liste=null;
		try
		{
			Session session = HibernateUtil.currentSession();
			Transaction tx = session.beginTransaction();
			Query hql = session.createQuery(
					"FROM MessageRecu as mes WHERE mes.eleve.idEleve like :idEl" );
			hql.setInteger("idEl", IdEnfant);			
			liste = hql.list();			
			tx.rollback();
			HibernateUtil.closeSession();
		}
		catch (Exception e) {
			// TODO: handle exception
			HibernateUtil.closeSession();
			e.printStackTrace();
		}
		return liste;	
		
		
	}
	
	
	public static String UnMessageRecu(Integer IdMessage){		
		List<MessageRecu> liste=null;
		String mess="";
		try
		{
			Session session = HibernateUtil.currentSession();
			Transaction tx = session.beginTransaction();
			Query hql = session.createQuery(
					"FROM MessageRecu as mes WHERE mes.idMessageRecu like :idMes" );
			hql.setInteger("idMes", IdMessage);			
			liste = hql.list();	
			
			if(liste.size()>0) mess=liste.get(0).getMessageRecu();
			
			tx.rollback();
			HibernateUtil.closeSession();
		}
		catch (Exception e) {
			// TODO: handle exception
			HibernateUtil.closeSession();
			e.printStackTrace();
		}
		return mess;		
	}
	
	public static void main(String[] args) {
		System.out.println(listeMessageRecu(1).get(0).getObjetMessageRecu());
		System.out.println(listeMessageRecu(1).get(0).getMessageRecu());
		System.out.println(UnMessageRecu(1));
		
	}
	
}
